\\ PARI/GP Sourcecode.

\\ Hint: For a best performance, use this script with GP2C.

subsequence(n,{B=10},{w=0})={ /*  Computes (storing them as a "t_VECSMALL" object) the arithmetic (numerical,
                                  decimal by default) for the first n!-1 terms of the sequence A217626
                                  if selector w=0 is used; Otherwise (for a nonzero w) computes a different
                                  analogous sequence obtained by replacing the lexicographic order for
                                  permutations (its underlying algorithm) with an specific alternative.
                             
                               It is noticed that such different order also induces the observed property
                               known as "autosimilarity", but scaling particular-case subsequences by a
                               predictable common factor which is a function of the used base. In detail:
                               A curious property of the resulting sequence, besides being symmetric,
                               their particular case subsequences according to n and B, are such that the GCD
                               of their elements is 1; Also let be 2<=m<n, then the m!-1 terms given by
                               subsequence(m,B,1) are present (n-m+1)! times among the n!-1 terms given by
                               subsequence(n,B,1) but scaled by a common factor of B^(n-m);
                               
                               Each particular case of the kind subsequence(n,B,1) has all its elements
                               divisible by B excepting those at positions 0 mod n!-1; Those exceptions
                               are often squarefree integers. 
                               
                               The conjecture 4 of A217626 applies directly "as is" to these other subsequences!
                               (of course, only if those described scaling factors are taken into account)
                               
                               Both Shmuel Zaks's algorithm and Narayana Pandit's algorithm have also in common
                               the property that given their complete output (the n! different permutations),
                               by adding to itself its reverse we get n!*n times the repetition of n-1 or n+1,
                               depending on the digits choice (either 0..n-1 or 1..n);
                                                             
                               Yet another possibility (not fully implemented here) consists of replacing any of
                               the mentioned algorithms by the "least adjacent changes" algorithm also known
                               as the Steinhaus-Johnson-Trotter algorithm, which induces a quite more
                               complicated pattern whose explanation escapes from the illustrative
                               purpose of this file.
                               
                               Additionally (not fully implemented here) the so called Heap's algorithm...
                               
                               For the analogous sequence to A217626 built using the Heap's algorithm,
                               particular case subsequences are self-contained either more or less times
                               depending on if what we are comparing is the subsequences themselves or
                               their absolute values. The "scale factor rule" also applies here, but this
                               time consider 2<m<=n instead.
                               
                           */ 
  my(x0:vecsmall,x:vecsmall,y:vecsmall,i:small,j:small,k:small,u:small,t:small,Q:vecsmall,bb:small,m:small,hm:small,z:vecsmall);
  if(n<2,n=2);
  bb=B;
  z=Vecsmall(0,n-1);
  i=#z;
  z[#z]=1;
  while(1<=i--,
    z[i]=z[i+1];
    z[i]*=bb);
  x=Vecsmall(0,n);
  i=0;
  while(i++<=#x,
    x[i]=i;
    x[i]--);
  m=1;
  i=1;
  while(i++<=n,m*=i);
  hm=m\2;
  m--;
  Q=Vecsmall(0,hm); \\ Actually just n!/2 are enough. The other part comes already determined by symmetry.
  k=0;
  while(k++<=hm,
   x0=x;
   if(w,              \\ Implements Shmuel Zaks's algorithm;
      u=1;
      r=k;
      while(u++<k+1,if(r%u,break,r\=u)); \\ These 3 initial lines are an adaptation from the Charles Greathouse IV 's A055881 program;
      j=u;
      j\=2;
      i=0;
      while(i++<=j,
       t=x[i];
        x[i]=x[1+u-i];
        x[1+u-i]=t)
   ,                  \\ Else: Implements Narayana Pandit's lexicographic order algorithm 
      i=#x-1;
      while((x[i]>=x[i+1])&&i,i--);
      j=#x;
      while((x[i]>=x[j])&&(j>i),j--);
      t=x[j];
      x[j]=x[i];
      x[i]=t;
      u=#x;
      u-=i;
      u\=2;
      u++;
      while(u--,
        t=x[i+u];
        x[i+u]=x[1+#x-u];
        x[1+#x-u]=t)        \\ Permutation algorithms implementation ends here.
   );
    y=x; \\ Computes the kth term for the selected sequence 
    r=1;
    y[1]-=x0[1];
    u=y[1];
    u*=z[1];
    Q[k]+=u;    
    while(r++<#y, 
      y[r]-= x0[r];        
      y[r]+=  y[r-1];
      u=y[r];
      u*=z[r];
      Q[k]+=u;
    );
    \\ Q[1+m-k]=Q[k];  ...The Symmetry was took into account. It allows to get it in the half of the time...
  );
  return(Q)}

 /*
   ============================
        A p p e n d i x
  ===========================
*/

Symmetry(s0)={ /* Returns (-)1 if the subsequence s0 is (anti)symmetric; Else returns zero. */
  my(c:small, t=(-1)^!vecsum(s0));
  c=0;
  while(c++<=#s0\2,
    if(s0[c]!=t*s0[1+#s0-c],return(0))
  );
  return(t)
}

/* Remember: Both Symmetry() and DetectIsBinA() use vectors. First transform the Vecsmall objects into vectors ( by using Vec() ) just before calling them. */ 

DetectIsBinA(B,A,{S=0},{useList=0})={
  /* Note: By returning -1 we mean error, either something was not yet implemented or invalid input operands A, a/o B were used. */
  if((type(A)!=type([]))||(type(B)!=type([])),return(-1));
  if(A==B,return(if(useList,List(1),B!=[]))); /* Due the subtlety of the empty sets: Are they in themselves? */
  my(widthB:small,widthA:small,distro:list);
  widthB=#B;
  widthA=#A;
  if(widthB>widthA,return(if(useList,List(),0)));
  /* */ S=Symmetry(B); /* */
  /* * / if(Symmetry(B)!=S,return(-1)); / * */
  if(useList,
    distro=List();
  );
  my(total:small,k:small,t:small,pass:small,half_widthB:small,yy:small);
  total=0;
  k=0;
  half_widthB=widthB\2;  
  widthB--;
  t=S/(abs(S)+!S); /* +!S prevents division by zero. */
  while(widthA>=widthB+k++,
    pass=1; 
    if(S,
      yy=0;
      for(y=k,k+half_widthB,
        if((A[y]!=B[yy++])||(A[y]!=t*A[2*k+widthB-y]),pass=0;break)
      )
    ,
      pass=A[k..k+widthB]==B;
    );
    total+=pass;
    if(pass&&useList,
      listput(distro,k)
    )
  );
  return(if(useList,distro,total))
}

vecgcd(v,{skipmod=1})={my(q=v[1]);for(k=2,#v,if((skipmod>1)&&!(k%skipmod),,q=gcd(q,v[k]);if(q==1,return(1))));return(q)}

vecgcddiv(v,x)={my(y=vecgcd(v,x));if(y>1,for(k=1,#v,if(k%x,v[k]/=y)));return(v)}

/* For comparison with: subsequence(n,B,0); */
wrongWayA217626_1(n,{B=10})={my(x0=vector(n,i,i),x,v=vector(n!-1),c);while(c++<=#v,x=numtoperm(n,c);v[c]+=fromdigits(x-x0, B)/(B-1);x0=x);return(Vecsmall(v))} /* Actually is correct! */
wrongWayA217626_2(n,{B=10})={my(x0=vector(n,i,i),x,v=vector(n!-1),c);while(c++<=#v,x=numtoperm(n,c);v[c]+=fromdigits(x-x0, B)/    9;x0=x);return(Vecsmall(v))} /* This must fail... */
wrongWayA217626_3(n,{B=10})={my(x0=vector(n,i,i),x,v=vector(n!-1),c);while(c++<=#v,x=numtoperm(n,c);v[c]+=fromdigits(x-x0)   /    9;x0=x);return(Vecsmall(v))} /* Must differ from way 1 */

/*
  
What can be taken for granted:

1) In general, arbitrary assignations for the way 2 must fail due divisibility.

2) subsequence(n,10,0) and the way 3 must coincide always for the same n;

3) In general, subsequence(n,B) and the way 1 must coincide always for the same ordered pair (n,B);

4) Therefore the soundness problem in the definition of A215940 (forcing one to restrict it to decimal) essentially consist in that when using "digits" greater than 9, the information cannot be retrieved properly due to the fact that the mapping unicity is lost among vectorial and arithmetic representations.

Note (about point 4): In the present context of a PARI/GP implementation we would say that if using vectors with at least an element equal or greater than B, then they exist at least two vectors that maps trough fromdigits() to the same integer, this could be called degeneracy in analogy to the same phenom in eigenvalues theory.

5) A necessary but not enough condition for the autosimilarity shown by A217626 and the alternative sequence generated by Sequence, is precisely the degeneracy for positional number system representations that occurs when they are used as digits those integers either negative or greater than the base.

6) Analogous considerations must apply for similar subsequences based upon other permutations algorithms.

*/

/* As its name suggests below */

whereVecsmallMismatch(x:vecsmall,y:vecsmall)={my(L:list);L=List();if(x!=y,for(k=1,min(#x,#y),if(x[k]!=y[k],listput(L,k))));return(Vecsmall(L))}

/* The same now for vectors. */

whereMismatch(x,y)={my(L:list);L=List();if(x!=y,for(k=1,min(#x,#y),if(x[k]!=y[k],listput(L,k))));return(Vec(L))}

/* Quick first differences for a "t_VECSMALL" object, then the same for vectors, and higher differences for vectors. */

vecsmallDiff1(v:vecsmall)={my(answer:vecsmall);answer=Vecsmall(0,#v-1);for(c=1,#v-1,answer[c]=v[c+1]-v[c]);return(answer)}

vecDiff1(v)=vector(#v-1,i,v[i+1]-v[i]);

listDiff1(L:list)=vector(#L-1,i,L[i+1]-L[i]);

vecDiff(v,k=1)={my(w=vector(#v-k,j,((-1)^(k%2))*sum(i=0,k,((-1)^(i%2))*binomial(k,i)*v[j+i])));w}  

  /*The following routine implements recursively the Steinhaus-Jhonson-Trotter algorithm trough a sieve based on modular arithmetic. Usage: Initial "d"iagonal: 0 "/" (default) or 1 "\"; "w"aving diagonals? 1 "yes" (default), or 0 "no"; Output "format" 1 "A vector of vectors" (default), or 0 "A huge vector" */
anSlowSJT_impl(n,d=0,w=1,format=1)={ /* Note: As it might be expected, the result is the n! permutations for all the digits (0..n-1) in base n */
  if(n<1,return([]));
  if(n==1,return([0]));
  if(n==2,if(d,if(format,return([[1,0],[0,1]]),return([1,0,0,1])),if(format,return([[0,1],[1,0]]),return([0,1,1,0]))));
  my(u=anSlowSJT_impl(n-1,d,w,0)); /* Internally we use only the output format zero. */
  my(c=1,sqr_n=n*n);
  d=!d;
  my(v=vector(n!*n,i,0*if(w&&!((i-1)%sqr_n),d=!d)+(n-1)*(!d*(((i%sqr_n)%(n-1)==1)&&(1!=(i%sqr_n)))+!!d*((!(i%sqr_n))||(1==((i%sqr_n)%n-(i%sqr_n)\n))))));
  for(k=0,(n-1)!-1,
    for(j=1,n,
      for(i=1,n-1,
        if(c<=#v,
          while(v[c],c++);
          v[c]=u[k*(n-1)+i];
          c++
        )
      )
    )
  );
  c*=0;
  if(format,return(vector(n!,y,vector(n,x,v[c++]))),return(v))
}
  /* Note(s): 1) Here the usage of divrem() was avoided intentionally.
              2) This is an experimental implementation: Probably much more slower than other choices. */

/* Examples (in the hope of waking your curiosity): Perform the following assignations, then print them...
  
    1) X=vecDiff1(apply(fromdigits,anSlowSJT_impl(3)))/9;
    2) Y=vecDiff1(apply(fromdigits,anSlowSJT_impl(4)))/9;
    3) Z=vecDiff1(apply(fromdigits,anSlowSJT_impl(5)))/9;
    
    Sample def. below, for exploration of the Heap's algorithm (A280318)
*/

/* Heap's algorithm iterative version ( adapted from the corresponding pseudocode at: en.wikipedia.org/wiki/Heap's_algorithm ) */

heap_perm_set(n)={
  my(A=vector(n,j,j-1),Q=vector(n!,jj,vector(n)),c=vector(n),t,i,k=1);
  Q[1]+=A;
  while(i<n,
    if(c[i+1]<i,
      if(!(i%2),
        t      = A[1];
        A[1]   = A[i+1];
        A[i+1] =    t
      ,
        t           = A[c[i+1]+1];
        A[c[i+1]+1] = A[i+1];
        A[i+1]      = t
      );
      Q[k++]+=A;
      c[i+1]++;
      i=0
    ,
      c[i+1]=0;
      i++
    )
  );
  return(Q)
}

test_a217626_alike_seq(n,{fmt=1},{B=10})={
  my(p=vecDiff1(vector(n!,i,numtoperm(n,i-1))));
  for(i=1,#p,p[i]=vector(#p[i]-1,j,vecsum(p[i][1..j])));
  if(fmt,for(i=1,#p,p[i]=fromdigits(p[i],B)));
  return(p);
}

heap_a217626_alike_seq(n,{fmt=1},{B=10})={
  my(p=vecDiff1(heap_perm_set(n)));
  for(i=1,#p,p[i]=vector(#p[i]-1,j,vecsum(p[i][1..j])));
  if(fmt,for(i=1,#p,p[i]=fromdigits(p[i],B)));
  return(p);
}

SJT_a217626_alike_seq(n,{fmt=1},{B=10},{d=0},{w=1})={
  my(p=vecDiff1(anSlowSJT_impl(n,d,w)));
  for(i=1,#p,p[i]=vector(#p[i]-1,j,vecsum(p[i][1..j])));
  if(fmt,for(i=1,#p,p[i]=fromdigits(p[i],B)));
  return(p);
}

 \\ (End) By: R. J. Cano; On: Apr 24 2017;
 